#include <stdbool.h>
#include <stdint.h>
#include "nrf.h"
#include "nrf_gpio.h"
#include "nrfx_qspi.h"
#include "nrf_lcd.h"
#include "nrf_delay.h"
#include "nrf_drv_qspi.h"
#include "nrf_log.h"
#include "nrf_drv_spi.h"
#include "board_cfg.h"


#define ILI9341_NOP         0x00
#define ILI9341_SWRESET     0x01
#define ILI9341_RDDID       0x04
#define ILI9341_RDDST       0x09

#define ILI9341_SLPIN       0x10
#define ILI9341_SLPOUT      0x11
#define ILI9341_PTLON       0x12
#define ILI9341_NORON       0x13

#define ILI9341_RDMODE      0x0A
#define ILI9341_RDMADCTL    0x0B
#define ILI9341_RDPIXFMT    0x0C
#define ILI9341_RDIMGFMT    0x0D
#define ILI9341_RDSELFDIAG  0x0F

#define ILI9341_INVOFF      0x20
#define ILI9341_INVON       0x21
#define ILI9341_GAMMASET    0x26
#define ILI9341_DISPOFF     0x28
#define ILI9341_DISPON      0x29

#define SH8501A0_CASET       0x2A
#define SH8501A0_PASET       0x2B
#define SH8501A0_RAMWR       0x2C
#define ILI9341_RAMRD       0x2E

#define ILI9341_PTLAR       0x30
#define ILI9341_MADCTL      0x36
#define ILI9341_PIXFMT      0x3A

#define ILI9341_FRMCTR1     0xB1
#define ILI9341_FRMCTR2     0xB2
#define ILI9341_FRMCTR3     0xB3
#define ILI9341_INVCTR      0xB4
#define ILI9341_DFUNCTR     0xB6

#define ILI9341_PWCTR1      0xC0
#define ILI9341_PWCTR2      0xC1
#define ILI9341_PWCTR3      0xC2
#define ILI9341_PWCTR4      0xC3
#define ILI9341_PWCTR5      0xC4
#define ILI9341_VMCTR1      0xC5
#define ILI9341_VMCTR2      0xC7
#define ILI9341_PWCTRSEQ    0xCB
#define ILI9341_PWCTRA      0xCD
#define ILI9341_PWCTRB      0xCF

#define ILI9341_RDID1       0xDA
#define ILI9341_RDID2       0xDB
#define ILI9341_RDID3       0xDC
#define ILI9341_RDID4       0xDD

#define ILI9341_GMCTRP1     0xE0
#define ILI9341_GMCTRN1     0xE1
#define ILI9341_DGMCTR1     0xE2
#define ILI9341_DGMCTR2     0xE3
#define ILI9341_TIMCTRA     0xE8
#define ILI9341_TIMCTRB     0xEA

#define ILI9341_ENGMCTR     0xF2
#define ILI9341_INCTR       0xF6
#define ILI9341_PUMP        0xF7

#define ILI9341_MADCTL_MY  0x80
#define ILI9341_MADCTL_MX  0x40
#define ILI9341_MADCTL_MV  0x20
#define ILI9341_MADCTL_ML  0x10
#define ILI9341_MADCTL_RGB 0x00
#define ILI9341_MADCTL_BGR 0x08
#define ILI9341_MADCTL_MH  0x04




#define CMD_MAX_WORD_COUNT              200
#define SHM_INST_CMD_WRITE				1
#define SHM_SEQ_FINISH_CODE				0


#define SPI_LCD_INSTANCE  0 /**< flash_spi instance index. */

#if 0
#define PIXEL_DRWA_NUM_MAX      200      //p0.24

uint8_t pixel_draw_buf[PIXEL_DRWA_NUM_MAX * 2 + 4] = {0};   //最大每次100个像素点，每个像素点颜色2bytes，再加4bytes命令信息

uint16_t pixel_draw_size = 0;
bool pixel_draw_start = false;
uint16_t pixel_draw_x0 = 0;
uint16_t pixel_draw_x1 = 0;
uint16_t pixel_draw_y0 = 0;
uint16_t pixel_draw_y1 = 0;
uint8_t pixel_draw_x0_offset = 0;   //为了4的整数倍，前面需要补的个数
uint8_t pixel_draw_x1_offset = 0;   //为了4的整数倍，后面需要补的个数
#endif



static volatile bool spi_xfer_done;  //SPI数据传输完成标志
static const nrf_drv_spi_t flash_spi = NRF_DRV_SPI_INSTANCE(SPI_LCD_INSTANCE);  /**< flash_spi instance. */

#define USE_QSPI 1

typedef struct SHM_CMD_DESC {
			 uint8_t inst_flag;
			 uint8_t delay;
			 uint8_t wordcount;
			const uint8_t payload[CMD_MAX_WORD_COUNT];
		} SHM_CMD_DESC;


SHM_CMD_DESC SH8601Z_1_78_INIT_POWER_ON[] = {

{SHM_INST_CMD_WRITE,5,1,{0x11}}, // SLEEPOUT
{SHM_INST_CMD_WRITE,1,5,{0x2A,0x00,0x00,0x00,0xC1}},
{SHM_INST_CMD_WRITE,1,5,{0x2B,0x00,0x00,0x01,0x6F}},
{SHM_INST_CMD_WRITE,1,3,{0x44,0x01,0x6F}},
{SHM_INST_CMD_WRITE,1,2,{0x35,0x00}},
{SHM_INST_CMD_WRITE,1,2,{0x3A,0x55}}, //0x55:RGB565, 0x77:RGB888
{SHM_INST_CMD_WRITE,25,2,{0x53,0x20}},
{SHM_INST_CMD_WRITE,0,1,{0x29}}, // DISPLAY ON
//{SHM_INST_CMD_WRITE,1,3,{0xC0, 0x5A, 0x5A}}, // DISPLAY ON
//{SHM_INST_CMD_WRITE,1,2,{0xBA, 0x81}}, // DISPLAY ON

{SHM_SEQ_FINISH_CODE,0,0,{0}},
};

SHM_CMD_DESC SH8601Z_1_78_POWER_OFF[] = {

{SHM_INST_CMD_WRITE,55,1,{0x28}}, 
{SHM_INST_CMD_WRITE,110,1,{0x10}}, 

{SHM_SEQ_FINISH_CODE,0,0,{0}},
};


#if 0

//写初始化调用此函数
void SHM_Reg_Write(SHM_CMD_DESC* cmd);
//SHM_Reg_Write(SH8601Z_1_78_INIT_POWER_ON);
//SHM_Reg_Write(SH8601Z_1_78_POWER_OFF);


static void  SHM_QspiWrite_1W(uint8_t data);
static void  SHM_QspiWrite_4W(uint8_t data);

//三种QSPI传输类型。
static void  SHM_WriteCmd_QSPI_02(uint8_t data);
static void  SHM_WriteData_QSPI_02(uint8_t data);

static void  SHM_WriteCmd_QSPI_32(uint8_t data);
static void  SHM_WriteData_QSPI_32(uint8_t data);

static void  SHM_WriteCmd_QSPI_12(uint8_t data);
static void  SHM_WriteData_QSPI_12(uint8_t data);

void sh8501a0_pixel_draw_by_line(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint8_t* color, uint16_t size);

void SHM_Reg_Write(SHM_CMD_DESC* cmd)
{
	
	uint16_t idx = 0;
	uint16_t wc;

	while (cmd[idx].inst_flag != SHM_SEQ_FINISH_CODE) {
		nrf_gpio_pin_clear(SH8501A0_QSPI_CSN_PIN);
		nrf_delay_us(1);
		for (wc = 0; wc < cmd[idx].wordcount; wc++) {
			if (wc==0){
				SHM_WriteCmd_QSPI_02(cmd[idx].payload[wc]);
			}
			else{
				SHM_WriteData_QSPI_02(cmd[idx].payload[wc]);
			}
		}

		nrf_delay_us(1);
		nrf_gpio_pin_set(SH8501A0_QSPI_CSN_PIN);
		if (cmd[idx].delay != 0) {
		nrf_delay_ms(cmd[idx].delay + 2);
		}
		idx++;
//		return;
	}
}

static void  SHM_QspiWrite_1W(uint8_t data)
{  
	unsigned char i;
	uint8_t MB=data;

	for(i=0; i<8; i++)			
	{  
		nrf_gpio_pin_clear(SH8501A0_QSPI_SCK_PIN);
		if( (MB&0x80)) 
            nrf_gpio_pin_set(SH8501A0_QSPI_IO0_PIN);
		else 
            nrf_gpio_pin_clear(SH8501A0_QSPI_IO0_PIN);
		nrf_gpio_pin_set(SH8501A0_QSPI_SCK_PIN);
		MB <<= 1;			
	}
    nrf_gpio_pin_clear(SH8501A0_QSPI_SCK_PIN);
}

static void  SHM_QspiWrite_4W(uint8_t data)
{  
	unsigned char i;
	uint8_t MB=data;
	for(i=0; i<2; i++)			
	{  
		nrf_gpio_pin_clear(SH8501A0_QSPI_SCK_PIN);

		if( ((MB>>4)&0x01))	
            nrf_gpio_pin_set(SH8501A0_QSPI_IO0_PIN);
		else 
            nrf_gpio_pin_clear(SH8501A0_QSPI_IO0_PIN);

		if( ((MB>>5)&0x01))	
            nrf_gpio_pin_set(SH8501A0_QSPI_IO1_PIN);
		else 
            nrf_gpio_pin_clear(SH8501A0_QSPI_IO1_PIN);

		if( ((MB>>6)&0x01))	
            nrf_gpio_pin_set(SH8501A0_QSPI_IO2_PIN);
		else 
            nrf_gpio_pin_clear(SH8501A0_QSPI_IO2_PIN);

		if( ((MB>>7)&0x01))	
            nrf_gpio_pin_set(SH8501A0_QSPI_IO3_PIN);
		else 
            nrf_gpio_pin_clear(SH8501A0_QSPI_IO3_PIN);

		nrf_gpio_pin_set(SH8501A0_QSPI_SCK_PIN);

		MB <<= 4;			
	}
}

static void  SHM_WriteCmd_QSPI_02(uint8_t data)
{  
	uint8_t transType=0x02;
	uint8_t sendData=data;

	SHM_QspiWrite_1W(transType);
	SHM_QspiWrite_1W(0x00);
	SHM_QspiWrite_1W(sendData);
	SHM_QspiWrite_1W(0x00);

}

static void  SHM_WriteData_QSPI_02(uint8_t data)
{  
	uint8_t sendData=data;

	SHM_QspiWrite_1W(sendData);
}

static void  SHM_WriteCmd_QSPI_32(uint8_t data)
{  

	uint8_t transType=0x32;
	uint8_t sendData=data;

	SHM_QspiWrite_1W(transType);
	SHM_QspiWrite_1W(0x00);
	SHM_QspiWrite_1W(sendData);
	SHM_QspiWrite_1W(0x00);
}

static void  SHM_WriteData_QSPI_32(uint8_t data)
{  
	uint8_t sendData=data;

	SHM_QspiWrite_4W(sendData);

}

static void  SHM_WriteCmd_QSPI_12(uint8_t data)
{  
	uint8_t transType=0x12;
	uint8_t sendData=data;

	SHM_QspiWrite_1W(transType);
	SHM_QspiWrite_4W(0x00);
	SHM_QspiWrite_4W(sendData);
	SHM_QspiWrite_4W(0x00);
}

static void  SHM_WriteData_QSPI_12(uint8_t data)
{  
	uint8_t sendData=data;

	SHM_QspiWrite_4W(sendData);
}
#endif

static void command_list(void)
{
	uint16_t idx = 0;
	uint16_t wc;

    uint8_t tx_data[20] = {0};
    uint8_t size = 0;
    
    nrf_gpio_pin_clear(SH8501A0_RST_PIN);
    nrf_delay_ms(10);
    nrf_gpio_pin_set(SH8501A0_RST_PIN);
    nrf_delay_ms(10);
    
	while (SH8601Z_1_78_INIT_POWER_ON[idx].inst_flag != SHM_SEQ_FINISH_CODE)
	{
    	size = 0;
    	
		nrf_delay_us(1);
		for (wc = 0; wc < SH8601Z_1_78_INIT_POWER_ON[idx].wordcount; wc++)
		{
			if (wc==0)
			{
				tx_data[size++] = 0x02;
				tx_data[size++] = 0;
				tx_data[size++] = SH8601Z_1_78_INIT_POWER_ON[idx].payload[wc];
				tx_data[size++] = 0;
			}
			else
			{
                tx_data[size++] = SH8601Z_1_78_INIT_POWER_ON[idx].payload[wc];
			}

		}

        nrf_drv_spi_transfer(&flash_spi, tx_data, size, NULL, 0);

		nrf_delay_us(1);

		if (SH8601Z_1_78_INIT_POWER_ON[idx].delay != 0)
		{
		    nrf_delay_ms(SH8601Z_1_78_INIT_POWER_ON[idx].delay);
		}
		idx++;
	}
}

void lcd_spi_event_handler(nrf_drv_spi_evt_t const* p_event, void* p_context)
{
//    spi_xfer_done = true;
    
    switch (p_event->type)
    {
        case NRF_DRV_SPI_EVENT_DONE:

            /* Switch state */
            spi_xfer_done = true;

            break;

        default:
            break;
    }
}

static ret_code_t hardware_init(void)
{
    ret_code_t err_code = NRF_SUCCESS;
#if 1
    nrf_gpio_cfg_output(SH8501A0_POWER_PIN);
    nrf_gpio_pin_clear(SH8501A0_POWER_PIN);
    nrf_delay_ms(100);
    nrf_gpio_pin_set(SH8501A0_POWER_PIN);

    nrf_gpio_cfg_output(SH8501A0_TOUCH_POWER_PIN);
    nrf_gpio_pin_clear(SH8501A0_TOUCH_POWER_PIN);
//    nrf_delay_ms(10);
#endif
    
    //TE PIN
    nrf_gpio_cfg_input(SH8501A0_TE_PIN, NRF_GPIO_PIN_NOPULL);

    //RESET PIN
    nrf_gpio_cfg_output(SH8501A0_RST_PIN);
    nrf_gpio_pin_clear(SH8501A0_RST_PIN);
    nrf_delay_ms(10);
    nrf_gpio_pin_set(SH8501A0_RST_PIN);
    nrf_delay_ms(10);

    //使用SPI驱动屏幕
    nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
    spi_config.ss_pin   = SH8501A0_QSPI_CSN_PIN;
    spi_config.miso_pin = SH8501A0_QSPI_IO1_PIN;
    spi_config.mosi_pin = SH8501A0_QSPI_IO0_PIN;
    spi_config.sck_pin  = SH8501A0_QSPI_SCK_PIN;

    spi_config.irq_priority = SPI_DEFAULT_CONFIG_IRQ_PRIORITY;
    spi_config.orc          = 0xFF;
    spi_config.frequency    = NRF_DRV_SPI_FREQ_8M;
    spi_config.mode         = NRF_DRV_SPI_MODE_0;
    spi_config.bit_order    = NRF_DRV_SPI_BIT_ORDER_MSB_FIRST;

    APP_ERROR_CHECK(nrf_drv_spi_init(&flash_spi, &spi_config, NULL, NULL));
    
    return err_code;
}

static ret_code_t sh8501a0_init(void)
{
    ret_code_t err_code;

    err_code = hardware_init();
    if (err_code != NRF_SUCCESS)
    {
        return err_code;
    }
//    nrf_delay_ms(10);
    
    command_list();

    return NRF_SUCCESS;
}

void sh8501a0_uninit(void)
{
    uint16_t idx = 0;
    uint16_t wc;

    uint8_t tx_data[20] = {0};
    uint8_t size = 0;

    while (SH8601Z_1_78_POWER_OFF[idx].inst_flag != SHM_SEQ_FINISH_CODE)
    {
    	size = 0;
        for (wc = 0; wc < SH8601Z_1_78_POWER_OFF[idx].wordcount; wc++)
        {
            if (wc==0)
            {
                tx_data[size++] = 0x02;
                tx_data[size++] = 0;
                tx_data[size++] = SH8601Z_1_78_POWER_OFF[idx].payload[wc];
                tx_data[size++] = 0;
            }
            else
            {
                tx_data[size++] = SH8601Z_1_78_POWER_OFF[idx].payload[wc];

            }
        }
        
		nrf_delay_us(1);

        if (SH8601Z_1_78_POWER_OFF[idx].delay != 0)
        {
            nrf_delay_ms(SH8601Z_1_78_POWER_OFF[idx].delay);
        }
        idx++;
    }
    
    nrf_delay_ms(10);
    nrf_gpio_pin_clear(SH8501A0_RST_PIN);

    nrf_drv_spi_uninit(&flash_spi);
    nrf_gpio_pin_set(SH8501A0_POWER_PIN);

    nrf_gpio_cfg_input(SH8501A0_QSPI_CSN_PIN, NRF_GPIO_PIN_PULLUP);
    nrf_gpio_cfg_input(SH8501A0_QSPI_SCK_PIN, NRF_GPIO_PIN_PULLUP);
    nrf_gpio_cfg_input(SH8501A0_QSPI_IO0_PIN, NRF_GPIO_PIN_PULLUP);
    nrf_gpio_cfg_input(SH8501A0_QSPI_IO1_PIN, NRF_GPIO_PIN_PULLUP);
    nrf_gpio_cfg_input(SH8501A0_QSPI_IO2_PIN, NRF_GPIO_PIN_PULLUP);
    nrf_gpio_cfg_input(SH8501A0_QSPI_IO3_PIN, NRF_GPIO_PIN_PULLUP);

    nrf_gpio_cfg_input(SH8501A0_TP_RST, NRF_GPIO_PIN_PULLUP);
    nrf_gpio_cfg_input(SH8501A0_TP_INT, NRF_GPIO_PIN_PULLUP);

    nrf_gpio_cfg_input(SH8501A0_RST_PIN, NRF_GPIO_PIN_PULLUP);

    nrf_gpio_cfg_input(SH8501A0_TE_PIN, NRF_GPIO_PIN_PULLDOWN);

    nrf_gpio_cfg_input(SH8501A0_TOUCH_POWER_PIN, NRF_GPIO_PIN_PULLDOWN);

}

static void set_addr_window(uint16_t x_0, uint16_t y_0, uint16_t x_1, uint16_t y_1)
{
    ASSERT(x_0 <= x_1);
    ASSERT(y_0 <= y_1);

    uint8_t tx_data[50] = {0};
    uint8_t size = 0;

    uint16_t width_temp = x_1 - x_0;

    x_0 = x_0/4 * 4;
    x_1 = x_0 + width_temp;

    //CASET
    tx_data[size++] = 0x02;
    tx_data[size++] = 0;
    tx_data[size++] = SH8501A0_CASET;
    tx_data[size++] = 0;
    tx_data[size++] = (x_0 >> 8);
    tx_data[size++] = x_0;
    tx_data[size++] = (x_1 >> 8);
    tx_data[size++] = x_1;
    
    nrf_drv_spi_transfer(&flash_spi, tx_data, size, NULL, 0);

    //PASET
    size = 0;

    tx_data[size++] = 0x02;
    tx_data[size++] = 0;
    tx_data[size++] = SH8501A0_PASET;
    tx_data[size++] = 0;
    tx_data[size++] = (y_0 >> 8);
    tx_data[size++] = y_0;
    tx_data[size++] = (y_1 >> 8);
    tx_data[size++] = y_1;
    
    nrf_drv_spi_transfer(&flash_spi, tx_data, size, NULL, 0);


}

static void sh8501a0_pixel_draw(uint16_t x, uint16_t y, uint32_t color)
{
}

static void sh8501a0_rect_draw(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint32_t color)
{
}

static void sh8501a0_dummy_display(void)
{
    /* No implementation needed. */
}

static void sh8501a0_rotation_set(nrf_lcd_rotation_t rotation)
{
}

static void sh8501a0_display_invert(bool invert)
{
}

static lcd_cb_t sh8501a0_cb = {
    .height = SH8501A0_HEIGHT,
    .width = SH8501A0_WIDTH
};

const nrf_lcd_t nrf_lcd_sh8501a0 = {
    .lcd_init = sh8501a0_init,
    .lcd_uninit = sh8501a0_uninit,
    .lcd_pixel_draw = sh8501a0_pixel_draw,
    .lcd_rect_draw = sh8501a0_rect_draw,
    .lcd_display = sh8501a0_dummy_display,
    .lcd_rotation_set = sh8501a0_rotation_set,
    .lcd_display_invert = sh8501a0_display_invert,
    .p_lcd_cb = &sh8501a0_cb
};

#define SPLIT_SIZE          250 //拆分一次传输125个Pixel 
#define SPI_TX_BUF_SIZE     (SPLIT_SIZE + 4)    //发送spi数据的buf大小

static uint8_t lcd_spi_tx_buf[SPI_TX_BUF_SIZE] = {0};  //发送spi数据的buf

void sh8501a0_pixel_draw_by_line(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint8_t* color, uint16_t size)
{
//    NRF_LOG_INFO("sh8501a0_pixel_draw_by_line x0 = %d, y0 = %d, x1 = %d, y1 = %d, size = %d.", x0, y0, x1, y1, size);

    uint16_t height = y1 - y0 + 1;
    uint16_t width  = x1 - x0 + 1;

    //校验数据量是否匹配
    if ((height * width * 2 + 4) != size)
    {
        return;
    }
    
    set_addr_window(x0, y0, x1, y1);
    nrf_drv_spi_transfer(&flash_spi, color, size, NULL, 0);

}

void background_set(uint16_t color)
{
    uint16_t size = 0;

    uint16_t width   = SH8501A0_WIDTH;
    uint16_t height  = SH8501A0_HEIGHT;

    int tansTimes = (height * width * 2) / SPLIT_SIZE;
    uint32_t reminder_bytes = (height * width * 2) % SPLIT_SIZE;

    for (int transCnt = 0; transCnt < tansTimes; transCnt++)
    {
        size = 0;
        lcd_spi_tx_buf[size++] = 0x02;
        lcd_spi_tx_buf[size++] = 0;
        
        if(transCnt == 0)
        {
            lcd_spi_tx_buf[size++] = 0x2C;
        }
        else
        {
            lcd_spi_tx_buf[size++] = 0x3C;
        }
        
        lcd_spi_tx_buf[size++] = 0;
        
        for(uint16_t j = 0; j < (SPLIT_SIZE / 2); j++)
        {
            lcd_spi_tx_buf[size++] = color >> 8;
            lcd_spi_tx_buf[size++] = color;
        }
        
        nrf_drv_spi_transfer(&flash_spi, lcd_spi_tx_buf, size, NULL, 0);
    }

    if (reminder_bytes)
    {
        NRF_LOG_INFO("reminder_bytes = %u", reminder_bytes);
        size = 0;
        lcd_spi_tx_buf[size++] = 0x02;
        lcd_spi_tx_buf[size++] = 0;
        
        if(0 == tansTimes)
        {
            lcd_spi_tx_buf[size++] = 0x2C;
        }
        else
        {
            lcd_spi_tx_buf[size++] = 0x3C;
        }
        
        lcd_spi_tx_buf[size++] = 0;
        
        for(uint16_t j = 0; j < (reminder_bytes / 2); j++)
        {
            lcd_spi_tx_buf[size++] = color >> 8;
            lcd_spi_tx_buf[size++] = color;
        }
        
        nrf_drv_spi_transfer(&flash_spi, lcd_spi_tx_buf, size, NULL, 0);
    }
    
}

void screen_flush(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t color)
{
    uint16_t size = 0;

    //参数合法校验
    if ((x0 > x1) || (y0 > y1))
    {
        return;
    }
    
    uint16_t width   = x1 - x0 + 1;
    uint16_t height  = y1 - y0 + 1;

    //逐行设置
    for (uint16_t i = 0; i < height; i++)
    {
        size = 0;
        lcd_spi_tx_buf[size++] = 0x02;
        lcd_spi_tx_buf[size++] = 0;
        lcd_spi_tx_buf[size++] = 0x2C;
        lcd_spi_tx_buf[size++] = 0;

        for (uint16_t j = 0; j < width; j++)
        {
            lcd_spi_tx_buf[size++] = color >> 8;
            lcd_spi_tx_buf[size++] = color;
        }

        sh8501a0_pixel_draw_by_line(x0, (y0 + i), x1, (y0 + i), lcd_spi_tx_buf, size);
    }
}

void screen_flush_test(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t color)
{
    uint32_t total_size     = 0;
    uint16_t tx_size        = 0;
    uint16_t reminder_size  = 0;
    uint16_t tansTimes      = 0;

    //参数合法校验
    if ((x0 > x1) || (y0 > y1))
    {
        return;
    }

    
    set_addr_window(x0, y0, x1, y1);
    
    uint16_t width   = x1 - x0 + 1;
    uint16_t height  = y1 - y0 + 1;

    total_size = (height * width * 2);
    reminder_size = total_size % SPLIT_SIZE;

    //按照设定的包大小，分包发送
    tansTimes = total_size / SPLIT_SIZE;

    for (uint16_t transCnt = 0; transCnt < tansTimes; transCnt++)
    {
        tx_size = 0;
        lcd_spi_tx_buf[tx_size++] = 0x02;
        lcd_spi_tx_buf[tx_size++] = 0;
        
        if(transCnt == 0)
        {
            lcd_spi_tx_buf[tx_size++] = 0x2C;
        }
        else
        {
            lcd_spi_tx_buf[tx_size++] = 0x3C;
        }
        
        lcd_spi_tx_buf[tx_size++] = 0;
        
        for(uint16_t j = 0; j < (SPLIT_SIZE / 2); j++)
        {
            lcd_spi_tx_buf[tx_size++] = color >> 8;
            lcd_spi_tx_buf[tx_size++] = color;
        }

        nrf_drv_spi_transfer(&flash_spi, lcd_spi_tx_buf, tx_size, NULL, 0);
    }

    //有剩余不足1包的，单独发送一次
    if (reminder_size)
    {
        tx_size = 0;
        lcd_spi_tx_buf[tx_size++] = 0x02;
        lcd_spi_tx_buf[tx_size++] = 0;
        
        if(tansTimes == 0)
        {
            lcd_spi_tx_buf[tx_size++] = 0x2C;
        }
        else
        {
            lcd_spi_tx_buf[tx_size++] = 0x3C;
        }
        
        lcd_spi_tx_buf[tx_size++] = 0;
        
        for(uint16_t j = 0; j < (reminder_size / 2); j++)
        {
            lcd_spi_tx_buf[tx_size++] = color >> 8;
            lcd_spi_tx_buf[tx_size++] = color;
        }

        nrf_drv_spi_transfer(&flash_spi, lcd_spi_tx_buf, tx_size, NULL, 0);
    }
    
}


void lcd_display_img(uint16_t x, uint16_t y, uint16_t img_height, uint16_t img_width, uint8_t* imp_buf, uint8_t hiden)
{
    uint32_t tx_size = 0;

    uint32_t color_index = 0;

    //设置图片位置
    set_addr_window(x, y, (x + img_width - 1), (y + img_height - 1));

    lcd_spi_tx_buf[tx_size++] = 0x02;
    lcd_spi_tx_buf[tx_size++] = 0;
    lcd_spi_tx_buf[tx_size++] = 0x2C;
    lcd_spi_tx_buf[tx_size++] = 0;

    //逐行获取图片信息
    for (uint16_t i = 0; i < img_height; i++)
    {

        for (uint16_t j = 0; j < img_width; j++)
        {
            //隐藏不显示
            if (hiden)
            {
                //逐个像素填写颜色
                lcd_spi_tx_buf[tx_size++] = 0x00;
                lcd_spi_tx_buf[tx_size++] = 0x00;
            }
            else
            {
                //逐个像素填写颜色
                lcd_spi_tx_buf[tx_size++] = imp_buf[color_index++];
                lcd_spi_tx_buf[tx_size++] = imp_buf[color_index++];
            }

            //达到spi最大能发送的字节数
            if (tx_size >= SPLIT_SIZE + 4)
            {
                nrf_drv_spi_transfer(&flash_spi, lcd_spi_tx_buf, tx_size, NULL, 0);

                tx_size = 0;
                
                lcd_spi_tx_buf[tx_size++] = 0x02;
                lcd_spi_tx_buf[tx_size++] = 0;
                lcd_spi_tx_buf[tx_size++] = 0x3C;
                lcd_spi_tx_buf[tx_size++] = 0;
            }
        
        }
    }

    //发送最后一组数组
    if (tx_size > 4)
    {
        nrf_drv_spi_transfer(&flash_spi, lcd_spi_tx_buf, tx_size, NULL, 0);
    }
    
}


